package com.gitee.wsl.security.hash


import com.gitee.wsl.ext.base.currentTimeMillis
import com.gitee.wsl.struct.generator.Generator
import com.gitee.wsl.struct.generator.nextString
import kotlin.random.Random
import kotlin.test.Test
import kotlin.test.assertEquals


/**
 * https://gitee.com/chinabugotech/hutool/pulls/532
 */
class MetroHashTest {
    @Test
    fun testEmpty() {
        assertEquals("31290877cceaea29", (MetroHash.hash64("".encodeToByteArray(), 0)).toHexString())
    }

    @Test
    fun metroHash64Test() {
        val str = "我是一段测试123".encodeToByteArray()
        val hash64: Long = MetroHash.hash64(str)
        assertEquals(62920234463891865L, hash64)
    }

    @Test
    fun metroHash128Test() {
        val str = "我是一段测试123".encodeToByteArray()
        val hash128: LongArray = MetroHash.hash128(str).longArray
        assertEquals(4956592424592439349L, hash128[0])
        assertEquals(6301214698325086246L, hash128[1])
    }

    /**
     * 数据量越大 MetroHash 优势越明显，
     */
    @Test
    //@Disabled
    fun bulkHashing64Test() {
        val strArray: Array<String> = randomStringArray
        val startCity: Long = currentTimeMillis
        for (s in strArray) {
            CityHash.hash64(s.encodeToByteArray())
        }
        val endCity: Long =currentTimeMillis

        val startMetro: Long =currentTimeMillis
        for (s in strArray) {
            MetroHash.hash64(s.encodeToByteArray())
        }
        val endMetro: Long =currentTimeMillis

       println("metroHash =============" + (endMetro - startMetro))
       println("cityHash =============" + (endCity - startCity))
    }


    /**
     * 数据量越大 MetroHash 优势越明显，
     */
    @Test
    //@Disabled
    fun bulkHashing128Test() {
        val strArray: Array<String> = randomStringArray
        val startCity: Long =currentTimeMillis
        for (s in strArray) {
            CityHash.hash128(s.encodeToByteArray())
        }
        val endCity: Long =currentTimeMillis

        val startMetro: Long =currentTimeMillis
        for (s in strArray) {
            MetroHash.hash128(s.encodeToByteArray())
        }
        val endMetro: Long =currentTimeMillis

       println("metroHash =============" + (endMetro - startMetro))
       println("cityHash =============" + (endCity - startCity))
    }


    companion object {
        private val randomStringArray: Array<String>
            get() {
                val result: Array<String> = Array(10000000){ Random.nextString(Random.nextInt(64), Generator.Base64) }

                return result
            }
    }
}
